#!/bin/sh

WORK="$PWD/nvm_download-url-credentials-work.$$"
TEST_BIN="$WORK/bin"
ARGV_LOG="$WORK/argv.log"
STDIN_LOG="$WORK/stdin.log"
OLDHOME="$HOME"

cleanup() {
  unset -f die cleanup nvm_has_executable
  unset NVM_AUTH_HEADER NVM_NODEJS_ORG_MIRROR WGET_IS_GNU FAKE_EXIT
  rm -rf "$WORK"
  export PATH="$OLDPATH"
  HOME="$OLDHOME"
  export HOME
}
die () { echo "$@" ; cleanup ; exit 1; }

\. ../../../nvm.sh

\. ../../common.sh

OLDPATH="$PATH"

# fake curl: record every argument, and the config it reads from stdin
make_fake_curl "$TEST_BIN" 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"
: > "$STDIN_LOG"
for a in "$@"; do if [ "$a" = --config ]; then cat > "$STDIN_LOG"; fi; done'
VERSION_MESSAGE="curl 7.99.0 (fake)"

# fake wget: GNU Wget when $WGET_IS_GNU is set, and like BusyBox's otherwise;
# record every argument, and the URLs it reads from stdin
{
  echo '#!/bin/sh'
  echo 'if [ "$1" = --version ]; then'
  echo '  [ -n "${WGET_IS_GNU-}" ] || exit 1'
  echo '  echo "GNU Wget 1.21.2 built on linux-gnu."; exit 0'
  echo 'fi'
  echo 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"'
  echo ': > "$STDIN_LOG"'
  echo 'for a in "$@"; do if [ "$a" = --input-file=- ]; then cat > "$STDIN_LOG"; fi; done'
  echo 'exit "${FAKE_EXIT:-0}"'
} > "$TEST_BIN/wget"
chmod +x "$TEST_BIN/wget"

mkdir -p "$WORK/home"
HOME="$WORK/home"
export HOME ARGV_LOG STDIN_LOG
export PATH="$TEST_BIN:$OLDPATH"

NVM_NODEJS_ORG_MIRROR='https://ci-bot:s3cr3t@mirror.example/dist'
export NVM_NODEJS_ORG_MIRROR
URL="${NVM_NODEJS_ORG_MIRROR}/index.tab"

assert_not_an_argument() {
  ! grep -q 's3cr3t' "$ARGV_LOG" || die "${1}: the mirror password is an argument; got: $(cat "$ARGV_LOG")"
}

# given curl
nvm_download -L -s "$URL" -o - || die 'curl: nvm_download returned nonzero'
assert_not_an_argument 'curl'
EXPECTED='-q
--fail
-L
-s
-o
-
--config
-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "url = \"${URL}\"" ] || die "curl: expected the URL in its config, got >$(cat "$STDIN_LOG")<"

# given a header as well, both go in the one config
NVM_AUTH_HEADER='Bearer test-token'
export NVM_AUTH_HEADER
nvm_download -L -s "$URL" -o - || die 'curl with header: nvm_download returned nonzero'
assert_not_an_argument 'curl with header'
[ "$(grep -c -Fxe '--config' "$ARGV_LOG")" = 1 ] || die "curl with header: expected one --config, got >$(cat "$ARGV_LOG")<"
EXPECTED="header = \"Authorization: Bearer test-token\"
url = \"${URL}\""
[ "$(cat "$STDIN_LOG")" = "$EXPECTED" ] || die "curl with header: expected config >$EXPECTED<, got >$(cat "$STDIN_LOG")<"
unset NVM_AUTH_HEADER

# given a quote or a backslash, they are escaped within curl's quoted string
nvm_download -L -s "${NVM_NODEJS_ORG_MIRROR}/a\"b\\c" -o - || die 'curl, quote: nvm_download returned nonzero'
assert_not_an_argument 'curl, quote'
[ "$(cat "$STDIN_LOG")" = "url = \"${NVM_NODEJS_ORG_MIRROR}/a\\\"b\\\\c\"" ] || die "curl, quote: got config >$(cat "$STDIN_LOG")<"

# given a line break, the URL cannot add a line to the config
nvm_download -L -s "$(printf '%s\noutput = "/tmp/nvm-pwned' "$URL")" -o - || die 'curl, line break: nvm_download returned nonzero'
! grep -Fxqe '--config' "$ARGV_LOG" || die "curl, line break: got config >$(cat "$STDIN_LOG")<"

# given a URL without credentials, it stays an argument
nvm_download -L -s 'https://nodejs.org/dist/index.tab' -o - || die 'curl, no credentials: nvm_download returned nonzero'
EXPECTED='-q
--fail
-L
-s
https://nodejs.org/dist/index.tab
-o
-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl, no credentials: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"

# given only GNU Wget
nvm_has_executable() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }
WGET_IS_GNU=1
export WGET_IS_GNU
nvm_download -L -C - --progress-bar "$URL" -o "$WORK/out" || die 'GNU Wget: nvm_download returned nonzero'
assert_not_an_argument 'GNU Wget'
EXPECTED="-c
--progress=bar
-O
$WORK/out
--input-file=-"
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"

# and its exit code is kept
FAKE_EXIT=8
export FAKE_EXIT
nvm_download -L -s "$URL" -o -
EXIT_CODE=$?
unset FAKE_EXIT
[ "$EXIT_CODE" = 8 ] || die "GNU Wget: expected exit code 8, got $EXIT_CODE"

# given a header as well, which goes through the same-origin redirect check
NVM_AUTH_HEADER='Bearer test-token'
export NVM_AUTH_HEADER
nvm_download -L -s "$URL" -o - || die 'GNU Wget with header: nvm_download returned nonzero'
assert_not_an_argument 'GNU Wget with header'
EXPECTED='--max-redirect=0
-q
-O
-
--input-file=-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget with header: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget with header: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"
unset NVM_AUTH_HEADER

# given a wget that reads no URL from stdin, like BusyBox's, the URL stays an argument
unset WGET_IS_GNU
nvm_download -L -s "$URL" -o - || die 'other wget: nvm_download returned nonzero'
EXPECTED="-q
${URL}
-O
-"
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "other wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"

cleanup
